package com.ymttest.database.mongo.shenqi;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import com.google.common.collect.Lists;
import com.shop2cn.iapi.sqoptrade.parameter.enums.OrderStatusEnum;
import com.shop2cn.iapi.sqsynctrade.paremeter.mongo.ProxyIndex;
import com.ymttest.database.mongo.bean.shenqi.SellerTotalSalesAmount;
import com.ymttest.database.mongo.config.MongoProxyOrderConfig;
import com.ymttest.database.mongo.config.ProxyIndexField;
import com.ymttest.database.mongo.config.SchemaField;

public class ProxyOrderIndexQueryWapper {

	private MongoTemplate mongoProxyTemplate;

	public ProxyOrderIndexQueryWapper() {
		try {
			mongoProxyTemplate = new MongoProxyOrderConfig()
					.mongoProxyOrderTemplate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String getCollectionName(long userId) {
		long n = userId % 100;
		return SchemaField.Proxy + n;
	}

	public <T> List<T> find(Query query, Integer userId, Class<T> outputType) {
		return mongoProxyTemplate.find(query, outputType,
				getCollectionName(userId));
	}

	public <T> List<T> aggregate(long userId, Aggregation aggregation,
			Class<T> outputType) {

		return mongoProxyTemplate.aggregate(aggregation,
				getCollectionName(userId), outputType).getMappedResults();
	}

	public ProxyIndex findById(long orderId, long userId) {

		return this.findByIdWithQuery(new Query(), orderId, userId);
	}

	public boolean exists(Query query, long userId) {
		return mongoProxyTemplate.exists(query, getCollectionName(userId));
	}

	public long count(Query query, long userId) {
		return mongoProxyTemplate.count(query, getCollectionName(userId));
	}
	
	public long count(long sellerId, Criteria criteria) {
        return mongoProxyTemplate.count(Query.query(criteria), getCollectionName(sellerId));
    }

	public List<Long> queryOrderId(Query query, long userId) {

		query.fields().include(ProxyIndexField.OID);
		List<ProxyIndex> proxyIndexList = mongoProxyTemplate.find(query,
				ProxyIndex.class, getCollectionName(userId));

		return proxyIndexList.stream().map(e -> e.getOrderId())
				.collect(Collectors.toList());
	}

	public ProxyIndex findByIdWithQuery(Query query, long orderId, long userId) {
		// query.fields().include(ProxyIndexField.OID);
		query.addCriteria(Criteria.where(ProxyIndexField.OID).is(orderId))
				.addCriteria(Criteria.where(ProxyIndexField.SELLER_ID).is(userId));
		return mongoProxyTemplate.findOne(query, ProxyIndex.class,
				getCollectionName(userId));
	}
	
	public BigDecimal getSellerTotalSalesAmount(Long sellerId) {
		
		 // 卖家 id
        Criteria criteria = Criteria.where(ProxyIndexField.SELLER_ID).is(sellerId);
        // 店主角色
        criteria.and(ProxyIndexField.SELLER_ROLE).is(ProxyIndexField.ROLE_SHOP_OWNER);
        // 订单状态已支付未接单、已接单、已发货、已确认收货
        criteria.and(ProxyIndexField.STATUS).in(
                Lists.newArrayList(
                    OrderStatusEnum.ACCOUNT_PAID.getCode(),
                    OrderStatusEnum.SHIPPED.getCode(),
                    OrderStatusEnum.RECEIVED.getCode(),
                    OrderStatusEnum.SELLER_ACCEPT.getCode()
                )
        );
        
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group(ProxyIndexField.SELLER_ID).sum(ProxyIndexField.SHENQI_PAYMENT_RMB_AMOUNT).as("paidAmount")
        );
        
		List<SellerTotalSalesAmount> results = this.aggregate(
				sellerId, aggregation, SellerTotalSalesAmount.class);
		BigDecimal paidAmount = BigDecimal.ZERO;
		
		if (null != results && results.size() > 0) {
			
			paidAmount = results.get(0).getPaidAmount();
		}
        
        return paidAmount;
	}

	public static void main(String... args) {

//		Logger.info("ProxyIndex:"
//				+ new ProxyOrderIndexQueryWapper().findById(7423048699L, 500219690).getSellerId());
		
		System.out.println(new ProxyOrderIndexQueryWapper()
				.getSellerTotalSalesAmount(501442627L));
	}
}
